package com.yxs.school.api.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yxs.school.api.entity.Article;
import com.yxs.school.api.entity.ArticleVo;
import com.yxs.school.api.entity.Discuss;
import com.yxs.school.api.entity.DiscussVo;
import com.yxs.school.api.service.IDiscussService;
import com.yxs.school.api.service.impl.ArticleServiceImpl;
import com.yxs.school.result.R;
import com.yxs.school.util.DateUtil;
import com.yxs.school.util.Empty;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 文章 前端控制器
 * </p>
 *
 * @author whp
 * @since 2019-11-15
 */
@RestController
@RequestMapping("/api/article")
public class ArticleController {
    @Resource
    ArticleServiceImpl articleService;
    @Resource
    IDiscussService iDiscussService;
    @PostMapping("save")
    public R add(@RequestBody Article article) {
        article.setCreateTime(DateUtil.getDate());
        boolean data=articleService.save(article);
        if(data)
       return R.data(200,null,"添加成功") ;
        else   return R.data(400,null,"添加失败") ;

    }
    @DeleteMapping("delete")
    public R remove(@RequestBody List<Integer> ids){
        System.out.println(ids);
        boolean flag=articleService.removeByIds(ids);
        if(flag){
          return   R.data(200,"成功删除"+ids.size()+"条","成功");
        }else {
            return   R.data(400,null,"失败");
        }
    }
    @PatchMapping("update")
    @PutMapping("update")
    public R update(@RequestBody Article article){
        boolean flag=articleService.updateById(article);
        if(flag){
         return  R.data(200,null,"更新成功");
        }else {
            return  R.data(400,null,"更新失败");
        }
    }
//    1
    @GetMapping("list")
    public R list(@RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
                  @RequestParam(value = "size",required = false,defaultValue = "10")Integer size,
                  String keyword,@RequestParam(value = "type",required = false) Integer type,
                  String order,
                  String userName
    ){
        PageHelper.startPage(page,size);
        QueryWrapper<Article> wrapper =new QueryWrapper<>();
        PageInfo<ArticleVo> pageInfo=null;
        if (!Empty.isEmpty(keyword)) {
            wrapper.apply("where 1=1");
            wrapper.like("title",keyword)
                    .or(i->i.like("intro", keyword))
                    .or(i->i.like("main_info", keyword));

        }
        if (type!=null){
            if (Empty.isEmpty(keyword)){
                wrapper.apply("where 1=1");
            }
            wrapper.eq("article_type",type);
        }
        if (!Empty.isEmpty(userName)){
            if (type!=null&&!Empty.isEmpty(keyword))
            wrapper.eq("auther",userName);
            else {
                wrapper.apply("where 1=1");
                wrapper.eq("auther",userName);
            }
        }
        if(order!=null&&!"".equals(order)){
            wrapper.orderByDesc("create_time");
        }
        pageInfo =new PageInfo<>(articleService.selectListByType(wrapper));
        if (pageInfo==null){
            return R.fail(400,"");
        }
        int sum = (int)pageInfo.getTotal();
        List<ArticleVo> list= pageInfo.getList();
        Map<String,Object> res=new HashMap<>(16);
        res.put("total",sum);
        res.put("list",list);
        return R.data(200,res,"成功");
    }
    @GetMapping("detail")
    public R detail(Integer id) {
        Article article= articleService.getById(id);
        if (article!=null) {
            List<DiscussVo> discuss=iDiscussService.selectDetailList(new QueryWrapper<Discuss>().apply("where 1=1")
                    .eq("article_id",article.getId()));
            article.setClick(article.getClick()+1);
            boolean flag =articleService.updateById(article);
            if (!flag)
                return R.fail(500,"系统错误");
            Map<String,Object> res =new HashMap<>(16);
            res.put("article",article);
            res.put("discuss",discuss);
            return R.data(200,res,"");
        }
        return R.data(200,article,"");
    }

}
